package com.example.sunlight.wzoneunicom;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by SunLight on 2017/7/29.
 */

public class Wifis extends View {
    SizeChange sizeChange;

    int[] i;
    boolean[] b;
    List<String[]> level;
    List<int[]> screen;
    Randoms randoms;
    WifiStrength wifiStrength;
    Context context;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    Wifis.this.invalidate();
                    break;
                case 0x456:
                    Wifis.this.invalidate();
                    break;
            }
        }
    };

    public Wifis(Context context, WifiStrength wifiStrength, Activity activity) {
        super(context);
        this.context = context;
        this.wifiStrength = wifiStrength;
        this.level = wifiStrength.Strength(context);
        screen = new ArrayList<>();
        i = new int[this.level.size()];
        b = new boolean[this.level.size()];
        randoms = new Randoms(activity);
        sizeChange = new SizeChange();
        sizeChange.start();
        for (int j = 0; j < this.level.size(); j++) {
            screen.add(new int[]{randoms.WRandom(), randoms.HRandom()});
            new RingSize(j, i, b, this.level).start();
        }

    }


    public Wifis(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Wifis(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int kit1, kit, kit2, kit3, kit4, kit5, kit6;
    int size, size2, size3, size4, size5, size6;
    int num = 255, num2 = 255, num3 = 255, num4 = 255, num5 = 255, num6 = 255;
    boolean sate, sate2, sate3, sate4, sate5;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate((getWidth() + getPaddingLeft() - getPaddingRight()) / 2,
                (getHeight() + getPaddingTop() - getPaddingBottom()));
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(20);

        kit1 = Color.argb(200, 0, 163, 255);
        paint.setColor(kit1);
        canvas.drawCircle(0, 0, 80, paint);

        kit = Color.argb(num, 0, 163, 255);
        paint.setColor(kit);
        canvas.drawCircle(0, 0, 80 + size, paint);

        kit2 = Color.argb(num2, 0, 163, 255);
        paint.setColor(kit2);
        canvas.drawCircle(0, 0, 80 + size2, paint);

        kit3 = Color.argb(num3, 0, 163, 255);
        paint.setColor(kit3);
        canvas.drawCircle(0, 0, 80 + size3, paint);

        kit4 = Color.argb(num4, 0, 163, 255);
        paint.setColor(kit4);
        canvas.drawCircle(0, 0, 80 + size4, paint);

        kit5 = Color.argb(num5, 0, 163, 255);
        paint.setColor(kit5);
        canvas.drawCircle(0, 0, 80 + size5, paint);

        kit6 = Color.argb(num6, 0, 163, 255);
        paint.setColor(kit6);
        canvas.drawCircle(0, 0, 80 + size6, paint);


        Paint paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(3);
        paint2.setTextSize(15);
        for (int j = 0; j < level.size(); j++) {
            kit1 = Color.argb(70, 0, 163, 255);
            paint2.setColor(kit1);
            canvas.drawCircle(screen.get(j)[0], screen.get(j)[1], 5 + i[j], paint2);
            kit1 = Color.argb(255, 0, 163, 255);
            paint2.setColor(kit1);
            canvas.drawCircle(screen.get(j)[0], screen.get(j)[1], 4, paint2);
        }

    }

    class SizeChange extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(45);
                    if (size < 500) {
                        size += 2;
                        num -= 1;
                        if (size == 200) {
                            sate = true;
                        }
                    } else {
                        if (num > 0) {
                            num -= 1;
                            ;
                        } else {
                            size = 0;
                            num = 255;
                        }
                    }
                    if (sate) {
                        if (size2 < 500) {
                            size2 += 2;
                            num2 -= 1;
                            if (size2 == 200) {
                                sate2 = true;
                            }
                        } else {
                            if (num2 > 0) {
                                num2--;
                            } else {
                                size2 = 0;
                                num2 = 255;
                            }
                        }
                    }

                    if (sate2) {
                        if (size3 < 500) {
                            size3 += 2;
                            num3 -= 1;
                            if (size3 == 200) {
                                sate3 = true;
                            }
                        } else {
                            if (num3 > 0) {
                                num3--;
                            } else {
                                size3 = 0;
                                num3 = 255;
                            }
                        }
                    }
                    if (sate3) {
                        if (size4 < 500) {
                            size4 += 2;
                            num4 -= 1;
                            if (size4 == 200) {
                                sate4 = true;
                            }
                        } else {
                            if (num4 > 0) {
                                num4--;
                            } else {
                                size4 = 0;
                                num4 = 255;
                            }
                        }
                    }
                    if (sate4) {
                        if (size5 < 500) {
                            size5 += 2;
                            num5 -= 1;
                            if (size5 == 200) {
                                sate5 = true;
                            }
                        } else {
                            if (num5 > 0) {
                                num5--;
                            } else {
                                size5 = 0;
                                num5 = 255;
                            }
                        }
                    }
//                    if (sate5) {
//                        if (size6 < 420) {
//                            size6 += 2;
//                            num6 -= 1;
//                        } else {
//                            if (num6 >= 0) {
//                                num6--;
//                            } else {
//                                size6 = 0;
//                                num6 = 200;
//                            }
//                        }
//                    }
                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }

    /**
     * 大小线程
     */
    class RingSize extends Thread {
        int[] i;
        int num;
        boolean[] bool;
        List<String[]> level;

        public RingSize(int num, int[] i, boolean[] bool, List<String[]> level) {
            this.i = i;
            this.num = num;
            this.bool = bool;
            this.level = level;
        }

        @Override
        public void run() {

            try {
                while (true) {
                    Thread.sleep(70);
                    if (bool[num] == false) {
                        if (i[num] <= (Integer.parseInt(level.get(num)[0])) / 10 + 10) {
                            i[num] += 1;
                        } else if (i[num] == (Integer.parseInt(level.get(num)[0])) / 10 + 10 + 1) {
                            bool[num] = true;
                        }
                        handler.sendEmptyMessage(0x123);

                    } else if (bool[num] == true) {
                        if (i[num] >= 0) {
                            i[num] -= 1;

                        } else if (i[num] == -1) {
                            bool[num] = false;
                        }
                        handler.sendEmptyMessage(0x123);
                    }

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
